﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Platform.ObjectModel.Design;
using Platform.Infrastructure;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json;

namespace Platform.ObjectModel.Serializers
{
	public sealed class DataObjectNameStreamer : DataObjectStreamer //: IFormatter
	{
		public DataObjectNameStreamer()
			: base( Globals.Formatters.DataObjectName )
		{ }

		public override object FromStream( Type valueType, byte[] stream )
		{
			if(valueType != typeof( DataObjectName))
				throw new InvalidOperationException( "Somente \"DataObjectNames\" podem ser deserializados com esse serializar." );
			return DataObjectName.Parse( Encoding.Default.GetString( stream ) );
		}

		public override byte[] ToStream( object value )
		{
			DataObjectName name = value as DataObjectName;

			if ( name == null )
				throw new InvalidOperationException( "Somente \"DataObjectNames\" podem ser serializados com esse serializar." );
			return Encoding.Default.GetBytes( name.FullName );
		}

		//#region IFormatter Members

		//public SerializationBinder Binder
		//{
		//    get;
		//    set;
		//}

		//public StreamingContext Context
		//{
		//    get;
		//    set;
		//}

		//public object Deserialize( System.IO.Stream serializationStream )
		//{
		//    byte[] data = new byte[ serializationStream.Length ];

		//    serializationStream.Read( data, 0, data.Length );
		//    return DataObjectName.Parse( Encoding.Default.GetString( data ) );
		//}

		//public void Serialize( System.IO.Stream serializationStream, object graph )
		//{
		//    DataObjectName name = graph as DataObjectName;

		//    if ( name == null )
		//        throw new InvalidOperationException( "Somente \"DataObjectNames\" podem ser deserializados com esse serializar." );

		//    byte[] data = Encoding.Default.GetBytes( name.FullName );
		//    serializationStream.Write( data, 0, data.Length );
		//}

		//public ISurrogateSelector SurrogateSelector
		//{
		//    get;
		//    set;
		//}

		//#endregion
	}


	
	
	//internal static class DocumentReaderExtensions
	//{


	//}
}
